﻿<%@ WebHandler Language="C#" Class="MonthPlanLookBoard" %>

using System;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.SessionState;
using System.IO;
using ReportBusiness;

public class MonthPlanLookBoard : IHttpHandler
{
    public bool IsReusable { get { return false; } }
    private uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public void ProcessRequest(HttpContext context)
    {
        Dictionary<string, object> para = new Dictionary<string, object>();
        if (context.Request.Form["type"] != null)
        {
            para["type"] = context.Request.Form["type"].ToString();
            para["currentPage"] = context.Request.Form["page"].ToString();
            para["pageSize"] = context.Request.Form["rows"].ToString();
            para["startdate"] = context.Request.Form["startdate"].ToString();
            para["enddate"] = context.Request.Form["enddate"].ToString();
            para["factoryname"] = context.Request.Form["factoryname"].ToString();
        }
        else
        {
            if (context.Request.QueryString["type"] != null)
            {
                para["type"] = context.Request.QueryString["type"].ToString();
                para["startdate"] = context.Request.QueryString["startdate"].ToString();
                para["enddate"] = context.Request.QueryString["enddate"].ToString();
                para["factoryname"] = context.Request.QueryString["factoryname"].ToString();
            }
            else
                return;
        }


        StringBuilder sqlsb = new StringBuilder();
        sqlsb.AppendLine(@"SELECT m.mfgordername,m.qty,m.plannedstartdate,m.plannedcompletiondate,
                    '' catchupdate,'' adjustdate,'' startdate,m.plannedcompletiondate completiondate,50 mfgpercent,
                    p.description productdesc,pb.productname 
                    FROM mfgorder m 
                    LEFT JOIN productbase pb1 ON pb1.productbaseid =m.productbaseid
                    LEFT JOIN product p ON p.productid = nvl(pb1.revofrcdid,m.productid)
                    LEFT JOIN productbase pb ON pb.productbaseid=p.productbaseid
                    LEFT JOIN factory f ON f.factoryid= m.factoryid
                    WHERE m.qty>0  ");
        if (para["factoryname"].ToString() != "全部")
        {
            sqlsb.AppendFormat(" AND f.factoryname='{0}'", para["factoryname"]);
        }
        if (para.ContainsKey("startDate"))
        {
            sqlsb.AppendFormat(" AND m.plannedcompletiondate > to_date('{0}','yyyy-mm-dd hh24:mi:ss')", para["startDate"].ToString());
        }
        if (para.ContainsKey("endDate"))
        {
            sqlsb.AppendFormat(" AND m.plannedcompletiondate < to_date('{0}','yyyy-mm-dd hh24:mi:ss')", para["endDate"].ToString());
        }

        if (para["type"].ToString() == "Record")
        {
            int pageCount = 0;
            int rowCount = 0;
            uMESPagingDataDTO retR = dal.GetPagingDataIns(sqlsb.ToString(), Convert.ToInt32(para["currentPage"]), Convert.ToInt32(para["pageSize"]));
            pageCount = Convert.ToInt32(retR.PageCount);
            rowCount = Convert.ToInt32(retR.RowCount);
            DataTable dtResult = retR.DBTable;
            foreach (DataColumn col in dtResult.Columns)
            {
                col.ColumnName = col.ColumnName.ToLower();
            }
            var json = JsonConvert.SerializeObject(dtResult);
            context.Response.ContentType = "text/json";
            json = "{\"data\":" + json.ToString()
            + ",\"count\":" + rowCount
            + ",\"code\":" + "0"
            + ",\"msg\":" + "0"
            + "}";

            context.Response.Write(json.ToString());
            context.Response.End();
        }
        else if (para["type"].ToString() == "Rate")
        {
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("planQty");
            dtResult.Columns.Add("finishedQty");
            dtResult.Columns.Add("notFinishedQty");
            DataRow dr = dtResult.NewRow();
            dr["planQty"] = 100;
            dr["finishedQty"] = 80;
            dr["notFinishedQty"] = 20;
            dtResult.Rows.Add(dr);

            dr = dtResult.NewRow();
            dr["planQty"] = 80;
            dr["finishedQty"] = 40;
            dr["notFinishedQty"] = 40;
            dtResult.Rows.Add(dr);
            foreach (DataColumn col in dtResult.Columns)
            {
                col.ColumnName = col.ColumnName.ToLower();
            }
            var json = JsonConvert.SerializeObject(dtResult);
            context.Response.ContentType = "text/json";
            context.Response.Write(json.ToString());
            context.Response.End();
        }

    }
}